using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MT.eVotePRS.Common;
using MT.eVotePRS.Model;

namespace MT.eVotePRS.UI
{
    public partial class frmCheckStudent : Form
    {
        StudentInfo studentInfo;
        InventoryTxn inventoryTxn;

        public frmCheckStudent()
        {
            InitializeComponent();
            studentInfo = new StudentInfo();
        }

        private void frmCheckStudent_Load(object sender, EventArgs e)
        {
            txtStudentID.Select();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private bool IsValidate()
        {
            bool ret = true;

            // student id
            if (string.IsNullOrEmpty(txtStudentID.Text.Trim()))
            {
                DialogBox.boxWarning("Invalid Student ID!", "Warning!");
                txtStudentID.Select();
                return false;
            }          
            return ret;
        }

        private void txtStudentID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                studentInfo.StudentNo = txtStudentID.Text.Trim();

                if (string.IsNullOrEmpty(studentInfo.StudentNo)
                    || studentInfo.StudentNo.Length < 8 || !Common.Helper.Helper.IsNumeric(studentInfo.StudentNo))
                {
                    DialogBox.boxWarning("Invalid Student ID!", "Warning!");
                    txtStudentID.SelectAll();
                    return;
                }

                lblStudentName.Text = string.Empty;
                lblIsGraduated.Text = string.Empty;
                lblHandset.Text = string.Empty;
                lblStatus.Text = string.Empty;
                lblDateTime.Text = string.Empty;

                // Student Status
                // ------------------------------
                // Nil
                // Checked-out (Handset ID shown)
                // Checked-in  (Handset ID shown)
                // Report Lost
                // Report Fault
                // ------------------------------

                StudentInfo student = new Presentation.Logic.StudentActions().FindStudentByNo(studentInfo.StudentNo);
                if (student != null && student.Id > 0)
                {
                    studentInfo = student;

                    lblShowBarcode.Text = studentInfo.Barcode;
                    lblStudentName.Text = studentInfo.StudentName;
                    lblIsGraduated.Text = studentInfo.IsGraduate ? "Yes" : "No";
                }
                else
                {
                    lblShowBarcode.Text = "Nil";
                    lblStudentName.Text = "Nil";
                    lblIsGraduated.Text = "Nil";
                }

                InventoryTxn inventTxn = new Presentation.Logic.InventoryTxnActions().CheckStudentStatus(studentInfo.StudentNo);
                if (inventTxn != null && inventTxn.Id > 0)
                {
                    inventoryTxn = inventTxn;

                    if (inventoryTxn.IsOverride)
                    {
                        lblHandset.Text = "Nil";
                        lblDateTime.Text = "Nil";
                        lblStatus.Text = "Nil";
                    }
                    else
                    {
                        if (inventoryTxn.Status == Presentation.Util.Constants.Status_CheckOut.Id
                            || inventoryTxn.Status == Presentation.Util.Constants.Status_Fault.Id
                            || inventoryTxn.Status == Presentation.Util.Constants.Status_Lost.Id
                            || inventoryTxn.Status == Presentation.Util.Constants.Status_CheckIn.Id)
                        {
                            lblHandset.Text = inventoryTxn.HandsetID;
                            lblDateTime.Text = inventoryTxn.TxnTime.ToString("yyyy/MM/dd HH:mm:ss");
                            lblStatus.Text = inventoryTxn.UserEvent;
                        }
                    }
                }
                else
                {
                    lblHandset.Text = "Nil";
                    lblDateTime.Text = "Nil";
                    lblStatus.Text = "Nil";

                    txtStudentID.Enabled = true;
                    txtStudentID.SelectAll();
                }
            }
            txtStudentID.SelectAll();
        }
    }
}